package com.open.pokergames.biz.views.groups

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import com.open.pokergames.R
import com.open.pokergames.base.activity.EventBusTitleBaseActivity
import com.open.pokergames.base.entity.AppPageData
import com.open.pokergames.biz.entity.GroupEntity
import com.open.pokergames.biz.views.groups.fragments.GroupMsgFragment
import com.open.pokergames.biz.views.groups.fragments.GroupRoomsFragment
import com.open.pokergames.comm.repository.entity.ResultInfo
import com.open.pokergames.texas.repository.entity.RoomEntity
import com.sinothk.android.utils.XUtils
import kotlinx.android.synthetic.main.group_actvity.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class GroupActivity : EventBusTitleBaseActivity() {

    private var group: GroupEntity? = null

    companion object {
        var groupId = ""
    }

    override fun onDestroy() {
        super.onDestroy()
        groupId = ""
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.group_actvity)

        group = intent.getSerializableExtra("itemData") as GroupEntity?
        groupId = group!!.id

        setViewTitle(group!!.groupName, R.drawable.comm_more_white) {
            XUtils.intent().openActivity(this@GroupActivity, GroupDetailActivity::class.java)
                .putStringExtra("groupId", group!!.id)
                .requestCode(600)
                .start()
        }
        initView()
    }


    private fun initView() {
        initTabView()
    }

    private fun initTabView() {
        val titleList = ArrayList<String>()
        titleList.add("消息")
        titleList.add("牌局")

        initTab(titleList, getFragments())
    }

    private fun getFragments(): List<Fragment> {
        val fragments: ArrayList<Fragment> = ArrayList()
        fragments.add(GroupMsgFragment())
        fragments.add(GroupRoomsFragment())
        return fragments
    }

    private fun initTab(titles: ArrayList<String>, fragments: List<Fragment>) {

        val fragmentPagerAdapter = object : FragmentPagerAdapter(supportFragmentManager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            override fun getCount(): Int {
                return fragments.size
            }

            override fun getItem(arg0: Int): Fragment {
                return fragments[arg0]
            }
        }

        viewPager.offscreenPageLimit = titles.size - 1
        viewPager.adapter = fragmentPagerAdapter
        //        pager.addOnPageChangeListener(this);

        scrollTab.setTitles(titles) //, 0, titles.size <= 4
//        scrollTab.setNumber(1, "9", View.VISIBLE)//设置数字红点
        scrollTab.setViewPager(viewPager)
        scrollTab.setOnTabListener { index, _ ->
            viewPager.setCurrentItem(index, true)
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<RoomEntity>>) {
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        when (requestCode) {
            600 -> {
                if (200 == resultCode) {
                    finish()
                }
            }
        }
    }
}